package com.xuecheng.media.api;

import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
@Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {
    private final MediaFileService mediaFileService;
    public MediaFilesController(MediaFileService mediaFileService) {
        this.mediaFileService = mediaFileService;
    }


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){
     Long companyId = 1232141425L;
     return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);
    }

    @ApiOperation("上传图片")
    @PostMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto uploadCoursePic(@RequestPart("filedata")MultipartFile filedata,
                                               @RequestParam(value = "objectName", required = false) String objectName)
            throws IOException {
        UploadFileParamsDto paramsDto = new UploadFileParamsDto();
        paramsDto.setFilename(filedata.getOriginalFilename());
        paramsDto.setFileSize(filedata.getSize());
        paramsDto.setFileType("001001");
        File tempFile = File.createTempFile("minio", ".tmp");
        filedata.transferTo(tempFile);
        UploadFileResultDto uploadFileResultDto = mediaFileService.uploadCoursePic(1232141425L,
                tempFile.getAbsolutePath(),
                paramsDto,
                objectName);
        // 上传文件后删除临时文件
        tempFile.delete();
        return uploadFileResultDto;
    }

    @ApiOperation("检测文件是否存在于MinIO中")
    @PostMapping("/upload/checkfile")
    public RestResponse<Boolean> checkFile(@RequestParam("fileMd5") String fileMd5) {
        return mediaFileService.checkFile(fileMd5);
    }

    @ApiOperation("检测上传分块文件")
    @PostMapping("/upload/checkchunk")
    public RestResponse<Boolean> checkChunk(@RequestParam("fileMd5") String fileMd5,
                              @RequestParam("chunk") int chunk) {
        return mediaFileService.checkChunk(fileMd5, chunk);
    }

    @ApiOperation("上传分块文件")
    @PostMapping("/upload/uploadchunk")
    public RestResponse uploadChunk(@RequestParam("file") MultipartFile file,
                                    @RequestParam("fileMd5") String fileMd5,
                                    @RequestParam("chunk") int chunk) throws Exception {
        File temp = File.createTempFile("minio",".tmp");
        file.transferTo(temp);
        RestResponse restResponse = mediaFileService.uploadChunk(fileMd5,
                chunk,
                temp.getAbsolutePath());
        temp.delete();
        return restResponse;
    }

    @ApiOperation("合并文件")
    @PostMapping("/upload/mergechunks")
    public RestResponse mergeChunks(@RequestParam("fileMd5") String fileMd5,
                               @RequestParam("fileName") String fileName,
                               @RequestParam("chunkTotal") int chunkTotal) {
        Long companyId = 1232141425L;
        return mediaFileService.mergeChunks(companyId, fileMd5, fileName, chunkTotal);
    }
}
